package com.wms.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.wms.entity.Form;
import com.wms.entity.Goods;
import com.wms.entity.Param;
import com.wms.entity.User;
import com.wms.mapper.GoodsMapper;
import com.wms.mapper.UserMapper;
import com.wms.service.GoodsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wms
 * @since 2022-10-15
 */
@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements GoodsService {
    @Resource
    private GoodsMapper goodsMapper;
    @Override
    public IPage pageCC(IPage<Goods> page, Wrapper wrapper) {
        return goodsMapper.pageCC(page,wrapper);
    }
    
    @Override
    public List<Goods> get(Param param){

        LambdaQueryWrapper<Goods> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(Goods::getName, "测试");
        List<Goods> list = goodsMapper.selectList(wrapper);
        String nameString = "测试名称";
        if ("1".equals("1")) {
            nameString = "测试名称更改2024";
        }
        /*
        //不能在lamda表达式里用中途被改变了值的变量nameString，重新定义na 来接收nameString的值也行，用for遍历就没有这种问题。返回的list中集合的字段也是 name="测试名称更改1"
        String na=nameString;
        list.stream().forEach(e->{
            e.setName(na);
        });
        */
        if ("123".equals("123")) {
            //list为null不能遍历集合，list的size=0可以遍历集合
            if (CollectionUtils.isNotEmpty(list)) {
                for (Goods po : list) { 
                    po.setName(nameString);
                }
            }
        }
        
        List<Goods> list1=list.stream().map(e->{
            e.setName("202466");
            return e;
        }).collect(Collectors.toList());
        
        LambdaQueryWrapper<Goods> wrapper1 = new LambdaQueryWrapper<>();
        //selectOne 只是取查询结果中的一条数据，如果查询结果的条数大于1就会报错。如果查询结果是0条或1条是不报错的
        wrapper1.eq(Goods::getName,String.valueOf(param.getStorage())).last("limit 1");
        Goods one = goodsMapper.selectOne(wrapper1);
/*
        list.stream().forEach(e->{
            if (StringUtils.isNotBlank(e.getTargetValueString()) && (e.getStorage().equals(11) || e.getStorage().equals(14))) {
                e.setTargetValueFLoat(new BigDecimal(e.getTargetValueString()).setScale(3,BigDecimal.ROUND_HALF_UP));
            }
        });*/
        return list1;
    }
    //"targetValue":是整数小数都可以接收
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveGoodsInfo(Form form) {
        Goods goods = new Goods(); 
        //如果入参是字符串15 dFloat=“15” 或者是整数15、甚至是浮点数15.230 ，debug发现 入参form中Double类型的dFloat 是接收不到字符串15或整数15的，即form.dFolat=null，goods.dFolat=null;之后的B=null
        //重新命名为myTestFloat就没这毛病了
        BeanUtils.copyProperties(form, goods);
        //BigDecimal类型数据转字符串，字符串数据保留3位小数
        if(ObjectUtils.isNotEmpty(form.getTargetValue())){
            goods.setTargetValueString(form.getTargetValue().setScale(3,BigDecimal.ROUND_HALF_UP).toString());
        }
        
        //Double B = goods.getMyTestFloat();
        //goods.setTargetValueString(B.toString()); //Double B=15.000 转字符串结果是15.0 ，小数点后只有一个0，不建议用Double字段作为入参
        this.save(goods);
        return true;
    }
}










